package com.woniuxy.service.impl;

import com.woniuxy.dao.ManagerLoginDao;
import com.woniuxy.entity.RbacManager;
import com.woniuxy.entity.ResponseEntity;
import com.woniuxy.service.ManagerLogionService;
import com.woniuxy.untils.JWTUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Set;
import java.util.concurrent.TimeUnit;

/**
 * @BelongsProject: java-85-course-code
 * @BelongsPackage: com.woniuxy.service.impl
 * @Author: LiTongMiao
 * @Date: 2022/2/24 10:12
 * @Description: TODO
 */
@Service
public class ManagerLoginServiceImpl implements ManagerLogionService {
    @Autowired(required = false)
    ManagerLoginDao managerLoginDao;
    @Autowired
    RedisTemplate redisTemplate;
    @Override
    public ResponseEntity login(RbacManager rbacManager) {
        RbacManager r = managerLoginDao.login(rbacManager);
        if (r.getId()!=null){
            //如果数据库有数据，则我们要使用JWTUtil创建token
            String token = JWTUtil.sign(rbacManager.getAccount(), rbacManager.getPassword());
            r.setToken(token);
            r.setPassword("");
            //通过token存入对应的所属对象
            redisTemplate.opsForValue().set(token,r);
            //设置token过期时间 模拟使用那0s
            redisTemplate.expire(token,30, TimeUnit.SECONDS);
            return new ResponseEntity(200,"登录成功",r);
        }else {
            return new ResponseEntity(403,"账号或密码错误",null);
        }
    }

    @Override
    public String findPassword(String username) {
        return  managerLoginDao.findPassword(username);
    }

    @Override
    public Set<String> findAllRoles(String username) {
        return managerLoginDao.findAllRoles(username);
    }

    @Override
    public Set<String> findAllPerms(String username) {
        return managerLoginDao.findAllPerms(username);
    }
}
